package com.woniu.apidriver.controller;

import com.woniu.apidriver.service.OrderService;
import com.woniu.internalcommon.constant.CommonStatusEnum;
import com.woniu.internalcommon.constant.IdentityConstants;
import com.woniu.internalcommon.dto.OrderInfo;
import com.woniu.internalcommon.dto.ResponseResult;
import com.woniu.internalcommon.dto.TokenResult;
import com.woniu.internalcommon.request.OrderRequest;
import com.woniu.internalcommon.util.JwtUtils;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api-order")
public class OrderController {

    @Resource
    private OrderService orderInfoService;
    /**
     * 司机抢单
     * @param order
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/grab")
    public ResponseResult grabOrder(@RequestBody OrderRequest order,
                                    HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("Authorization");
        //从token中获取信息
        TokenResult tokenResult = JwtUtils.parseToken(token);
        String identity = tokenResult.getIdentity();
        if (!identity.equals(IdentityConstants.DRIVER_IDENTITY)){
            return ResponseResult.fail(CommonStatusEnum.TOKEN_ERROR.getCode(),CommonStatusEnum.TOKEN_ERROR.getValue());
        }
        String phone = tokenResult.getPhone();
        Long orderId = order.getOrderId();
        //获取--去接乘客时，司机的经纬度
        String driverLongitude = order.getToPickUpPassengerLongitude();
        String driverLatitude = order.getToPickUpPassengerLatitude();

        return orderInfoService.grab(orderId,phone,driverLongitude,driverLatitude);
    }

    /**
     * 去接乘客
     * @param orderRequest
     * @return
     */

    @PostMapping("toPickUpPassenger")
    public ResponseResult toPickUpPassenger(@RequestBody OrderRequest orderRequest){
    return orderInfoService.toPickUpPassenger(orderRequest);
    }

    /**
     * 到达乘客上车点
     * @param orderRequest
     * @return
     */
    @PostMapping("/arrivedDeparture")
    public ResponseResult arrivedDeparture(@RequestBody OrderRequest orderRequest){
        return orderInfoService.arrivedDeparture(orderRequest);
    }

    /**
     * 司机接到乘客
     * @param orderRequest
     * @return
     */
    @PostMapping("/pick-up-passenger")
    public ResponseResult pickUpPassenger(@RequestBody OrderRequest orderRequest){
        return orderInfoService.pickUpPassenger(orderRequest);
    }

    /**
     * 乘客到达目的地，行程终止
     * @param orderRequest
     * @return
     */
    @PostMapping("/passenger-getoff")
    public ResponseResult passengerGetoff(@RequestBody OrderRequest orderRequest){
        return orderInfoService.passengerGetoff(orderRequest);
    }


    /**
     * 订单取消--司机
     * @param orderId
     * @return
     */
    @PostMapping("/cancel")
    public ResponseResult cancel(@RequestParam("orderId") Long orderId){
        return orderInfoService.cancel(orderId);
    }

    /**
     * 查看订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/detail")
    public ResponseResult<OrderInfo> detail(@RequestParam("orderId")Long orderId){
        return orderInfoService.detail(orderId);
    }

    /**
     * 查询订单当前状态--司机
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/current")
    public ResponseResult<OrderInfo> currentOrder(HttpServletRequest httpServletRequest){
        String authorization = httpServletRequest.getHeader("Authorization");
        TokenResult tokenResult = JwtUtils.parseToken(authorization);
        String identity = tokenResult.getIdentity();
        if (!identity.equals(IdentityConstants.DRIVER_IDENTITY)){
            return ResponseResult.fail(CommonStatusEnum.TOKEN_ERROR.getCode(),CommonStatusEnum.TOKEN_ERROR.getValue());
        }
        String phone = tokenResult.getPhone();

        return orderInfoService.currentOrder(phone,IdentityConstants.DRIVER_IDENTITY);
    }

}
